package com.ll.wechat.base.activity;

import java.util.Iterator;

import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.offline.OfflineMessageManager;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ll.wechat.R;
import com.ll.wechat.base.app.User;
import com.ll.wechat.base.utils.thread.ThreadPoolUtil;
import com.ll.wechat.chat.widget.FaceUtil;
import com.ll.wechat.database.DataBaseHelperUtil;
import com.ll.wechat.friends.activity.AddFriendsActivity;

/**
 * 主页面
 * 
 * @ClassName: MainActivity
 * @author yeliangliang
 * @date 2015-7-24 下午5:24:20
 */
public class MainActivity extends BaseActivity implements OnClickListener{
	private PopupWindow popupWindow ;
	private View view1 = null ;
	@Override
	public void initView() {
		setContentView(R.layout.activity_main);
		// 取消actionBar的图标
		getActionBar().setDisplayShowHomeEnabled(false);
	}

	@Override
	public void initData() {
		initEmojiFace();
		// 获取离线消息
		getOfflineMessage();
		// 设置聊天管理器
		setChatManager();
	}

	/**
	 * 初始化emoji表情
	 * 
	 * @author yeliangliang
	 * @date 2015-8-14 下午5:42:57
	 * @version V1.0
	 * @return void
	 */
	private void initEmojiFace() {
		Runnable runnable = new Runnable() {

			@Override
			public void run() {
				FaceUtil f = FaceUtil.getInstance(MainActivity.this);
				f.getEmojiList();
			}
		};
		ThreadPoolUtil.insertTaskToCatchPool(runnable);
	}

	/**
	 * 获取离线消息
	 * 
	 * @author yeliangliang
	 * @date 2015-8-11 下午5:21:09
	 * @version V1.0
	 * @return void
	 */
	private void getOfflineMessage() {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				OfflineMessageManager offlineManager = new OfflineMessageManager(User.getInstance()
						.getConnection());
				try {
					Iterator<Message> it = offlineManager.getMessages().iterator();
					// 干掉服务器中的离线消息
					offlineManager.deleteMessages();
					// 上线
					Presence presence = new Presence(Presence.Type.available);
					User.getInstance().getConnection().sendPacket(presence);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
		ThreadPoolUtil.insertTaskToCatchPool(r);
	}

	/**
	 * 设置聊天管理器
	 * 
	 * @author yeliangliang
	 * @date 2015-8-4 上午10:56:51
	 * @version V1.0
	 * @return void
	 */
	private void setChatManager() {
		Runnable runnable = new Runnable() {

			@Override
			public void run() {
				User.getInstance().setChatManager();
			}
		};
		ThreadPoolUtil.insertTaskToCatchPool(runnable);
	}

	// 返回键的监听
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			exitApp(MainActivity.this);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 创建ActionBar菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * ActionBar的点击监听
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.action_add:
			initPopWindow();
			break;

		default:
			break;
		}

		return super.onOptionsItemSelected(item);

	}

	@Override
	protected void onResume() {
		super.onResume();
		// 打开数据库
		DataBaseHelperUtil.getInstance(this).openDataBase();
	}

	@Override
	protected void onStop() {
		super.onStop();
		// 关闭数据库
		DataBaseHelperUtil.getInstance(this).closeDataBase();
	}

	/**
	 * 初始化添加好友下拉菜单
	 * 
	 * @author yeliangliang
	 * @date 2015年9月9日 下午5:22:32
	 * @version V1.0
	 * @return void
	 */
	private void initPopWindow() {
		if (popupWindow==null) {
			View view = LayoutInflater.from(this).inflate(R.layout.layout_popwindow_actionbar, null);
			view.setId(0001);
			view.setOnClickListener(this);
			ImageView uImageView = (ImageView) view.findViewById(R.id.img_delete);
			uImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.icon_action_add));
			TextView tv = (TextView) view.findViewById(R.id.tv_delete);
			tv.setText("添加好友");
			popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			view1 = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
			popupWindow.setOutsideTouchable(true);
			popupWindow.setBackgroundDrawable(new ColorDrawable());
		}
		if (popupWindow.isShowing()) {
			return ;
		}
		popupWindow.showAtLocation(view1, Gravity.TOP | Gravity.RIGHT, 0, 150);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case 0001:
			popupWindow.dismiss();
			Intent intent = new Intent(this,AddFriendsActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}
}
